/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.polarrose.wsf.controller;

import com.polarrose.wsf.action.WebServiceActionHandler;
import com.polarrose.wsf.stereotype.WebServiceAction;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

public class AnnotationWebServiceController extends AbstractWebServiceController
{
    private String name;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    //

    @Resource
    private ApplicationContext applicationContext;

    private final Map<String, WebServiceActionHandler> actionsByNameAndVersion = new HashMap<String,WebServiceActionHandler>();

    @PostConstruct
    public void initializeActions()
    {
        if (log.isInfoEnabled()) {
            log.info("Initializing AnnotationWebServiceController; name = " + name);
        }

        String[] beanNames = applicationContext.getBeanNamesForType(Object.class);
        for (String beanName : beanNames)
        {
            Class<?> actionType = AopUtils.getTargetClass(applicationContext.getBean(beanName));
            WebServiceAction webServiceAction = AnnotationUtils.findAnnotation(actionType, WebServiceAction.class);
            if (webServiceAction != null)
            {
                String webServiceName = webServiceAction.webServiceName();
                if ((name == null && webServiceName.compareTo("") == 0) || (name != null && name.compareTo(webServiceName) == 0))
                {
                    if (log.isInfoEnabled()) {
                        log.info("Registering web service action " + webServiceAction.name() + " with version " + webServiceAction.version());
                    }

                    String name = webServiceAction.name();
                    if (webServiceAction.version() != null) {
                        name = name + ":" + webServiceAction.version();
                    }

                    actionsByNameAndVersion.put(name, (WebServiceActionHandler) applicationContext.getBean(beanName));
                }
            }
        }
    }

    public WebServiceActionHandler getActionByNameAndVersion(String name, String version)
    {
        String key = name;
        if (version != null) {
            key = key + ":" + version;
        }
        return actionsByNameAndVersion.get(key);
    }
}
